package com.sdp.wz.controller;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.db.wz_output.entity.WzOutput;
import com.sdp.wz.db.wz_output.service.IWzOutputService;
import com.sdp.wz.db.wz_output_wz.entity.WzOutputWz;
import com.sdp.wz.db.wz_output_wz.service.IWzOutputWzService;
import com.sdp.wz.db.wz_serial_code.service.IWzSerialCodeService;
import com.sdp.wz.db.wz_warehouse_info.entity.WzWarehouseInfo;
import com.sdp.wz.db.wz_warehouse_info.service.IWzWarehouseInfoService;
import com.sdp.wz.handleImpl.WzBarCodeOutputHandleImpl;
import com.sdp.wz.utils.WzConstants;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzOutputController")
public class WzOutputController {

	@Autowired
	private Query query;
	
	@Autowired
	private IWzOutputService iWzOutputService;
	
	@Autowired
	private IWzSerialCodeService iWzSerialCodeService;
	
	@Autowired
	private IWzWarehouseInfoService iWzWarehouseInfoService;
	
	@Autowired
	private IWzOutputWzService iWzOutputWzService;
	
	@Autowired
	private WzBarCodeOutputHandleImpl wzBarCodeOutputHandleImpl;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWzOutputService, WzOutput.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody JSONObject obj) {
		if(obj==null) {
			return Result.error("出库信息为空");
		}
		WzOutput wzOutput = obj.getObject("wzOutput", WzOutput.class);
		
		if(wzOutput==null) {
			return Result.error("出库信息为空");
		}
		
		if(StringUtils.isEmpty(wzOutput.getWarehouseCode())) {
			return Result.error("仓库信息为空");
		}
		
		List<WzOutputWz> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzOutputWz")), WzOutputWz.class);
		if(wzList==null || wzList.size()==0) {
			return Result.error("出库的物资信息为空");
		}
		
		WzWarehouseInfo warehouse = iWzWarehouseInfoService.queryWarehouseInfoByCode(wzOutput.getWarehouseCode());
		if(warehouse==null) {
			return Result.error("仓库不存在");
		}
		
		if(StringUtils.isEmpty(wzOutput.getKeyId())) {
			String outputCode = iWzSerialCodeService.createSerialCode(WzConstants.流水号前缀_物资出库单.getCode());
			wzOutput.setOutputCode(outputCode);
			wzOutput.setCreateTime(LocalDateTime.now());
			wzOutput.setIsUse(WzConstants.否.getCode());
			wzOutput.setOutputTime(LocalDateTime.now());
			wzOutput.setOutputDate(LocalDate.now());
			wzOutput.setOrgNo(warehouse.getOrgNo());
			for(WzOutputWz wz : wzList) {
				wz.setOutputCode(outputCode);
			}
			iWzOutputService.save(wzOutput);
			iWzOutputWzService.saveBatch(wzList);
		}else {
			iWzOutputService.updateById(wzOutput);
			iWzOutputWzService.updateBatchById(wzList);
		}
		return Result.ok(wzOutput);
	}
	
	
	
	@PostMapping(value = "/commit")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> commit(@RequestBody JSONObject obj) {
		Result<?> r = this.save(obj);
		if(r==null || !r.isSuccess()) {
			return Result.error(r.getMessage());
		}
		WzOutput wzOutput = obj.getObject("wzOutput", WzOutput.class);
		List<WzOutputWz> wzList = JSONArray.parseArray(JSON.toJSONString(obj.get("wzOutputWz")), WzOutputWz.class);
		
		BusinessContext businessContext = new BusinessContext();
		businessContext.put(WzOutput.class, wzOutput);
		businessContext.put(WzOutputWz.class, wzList);
		wzBarCodeOutputHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok(wzOutput);
		}else {
			return Result.error(businessContext.getMessage());
		}
	}
}

